package com.bocai.yuweiwangluo.presenter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;

import com.bocai.yuweiwangluo.adapter.MyMatchListAdapter;
import com.bocai.yuweiwangluo.base.BaseView;
import com.bocai.yuweiwangluo.bean.MyMatchListBean;
import com.bocai.yuweiwangluo.net.MyOkHttpClient;
import com.bocai.yuweiwangluo.utils.SharePrefencesUtil;
import com.bocai.yuweiwangluo.utils.UrlData;
import com.bocai.yuweiwangluo.viewModel.IMyMatchListView;
import com.google.gson.Gson;


/**
 * Created by admin on 2015/12/16.
 */
public class MyMatchListPresenter
{
    private MyMatchListBean myMatchListBean;
    private static final int RESPONSE_SUCCEED = 1;
    private static final int RESPONSE_FAILED = 2;
    private IMyMatchListView myMatchListView;
    private Context context;
    private BaseView baseView;
    private MyMatchListAdapter myMatchListAdapter;
    public MyMatchListPresenter(Context context, IMyMatchListView myMatchListView, BaseView baseView)
    {
        this.context = context;
        this.myMatchListView = myMatchListView;
        this.baseView=baseView;
    }

    /**
     * 获取我的约战列表
     */
    public void getMyMatchList()
    {
        baseView.showLoading();
        MyOkHttpClient myOkHttpClient = new MyOkHttpClient(context,baseView);
        myOkHttpClient.getMyMatch(UrlData.BASEURL + UrlData.GETMATCH, myMatchListView.getmyPage() + "", SharePrefencesUtil.getUser_id(context), handler);
    }
    public MyMatchListBean getData()
    {

        return myMatchListBean;
    }
    private Handler handler = new Handler()
    {
        @Override
        public void handleMessage(Message msg)
        {
            super.handleMessage(msg);
            baseView.hideLoading();
            String result = (String) msg.obj;
            Gson gson = new Gson();
            myMatchListView.onLoad();
            try
            {
                switch (msg.what)
                {
                    case RESPONSE_SUCCEED:
                        Log.e("result", result);

                        myMatchListBean= gson.fromJson(result, MyMatchListBean.class);
                        myMatchListAdapter =new MyMatchListAdapter(context,myMatchListBean);
                        if(myMatchListBean.getData().size()==0&myMatchListView.getmyPage()==1)
                        {
                            myMatchListView.llnoContent().setVisibility(View.VISIBLE);
                            myMatchListView.getListView().setVisibility(View.GONE);
                        }else
                        {
                            myMatchListView.llnoContent().setVisibility(View.GONE);
                            myMatchListView.getListView().setVisibility(View.VISIBLE);
                            if(myMatchListView.getmyPage()==1)
                            {
                                myMatchListView.getListView().setAdapter(myMatchListAdapter);
                            }else
                            {
                                myMatchListAdapter.addData(myMatchListBean);
                            }
                            if(myMatchListBean.getTotal()<=myMatchListBean.getEach()*myMatchListView.getmyPage())
                            {
                                myMatchListView.getListView().setPullLoadEnable(false);
                            }
                        }
                        break;
                    case RESPONSE_FAILED:
                        baseView.showError(result);
                        myMatchListView.getllRefresh().setVisibility(View.VISIBLE);
                        break;
                }
            } catch (Exception e)
            {
            }
        }
    };
}